import 'package:baidu_ocr_plugin/baidu_ocr_plugin.dart';
import 'package:camera/camera.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/color.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/ht_appbar.dart';
import 'package:wallet_utils/log_util.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/report_widget/loading_utils.dart';
import 'package:wallet_utils/safe_bottom_view.dart';
import 'package:wallet_utils/text_style.dart';
import '../../mine/contentType.dart';
import 'logic.dart';

class IdentityCertificationPage extends StatelessWidget {
  final logic = Get.put(IdentityCertificationLogic());
  final state = Get
      .find<IdentityCertificationLogic>()
      .state;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
       logic.backPage();
        return Future.value(true);
      },
      child: Scaffold(
        backgroundColor: ColorManager.colorFFFFFF,
        appBar: htCommonAppbar(context, title: '身份认证',leadingAction: (){
          logic.backPage();
        }),
        bottomNavigationBar: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            GetBuilder<IdentityCertificationLogic>(builder: (logic) {
              return Container(
                color: ColorManager.colorF4F5F6,
                padding: EdgeInsets.only(left: 16.w, right: 16.w),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Padding(
                      padding: EdgeInsets.only(right: 5.w),
                      child: GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          logic.updateUserPrivary();
                        },
                        child: Container(
                          width: 16.w,
                          height: 16.w,
                          margin: const EdgeInsets.only(top: 4),
                          alignment: Alignment.center,
                          child: state.isAgreeUserPrivary
                              ? Image.asset(
                            "images/ic_agreement_checked.png",
                            width: 11.w,
                            height: 11.w,
                          )
                              : Image.asset(
                            "images/ic_agreement_unchecked.png",
                            width: 11.w,
                            height: 11.w,
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: Text.rich(
                        TextSpan(
                          children: <InlineSpan>[
                            TextSpan(
                                text: "我已阅读并同意",
                                style: TextStyleUtils.style999999(fontSize: 11.sp),
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () {}),
                            TextSpan(
                              text: "《个人信息查询授权书》",
                              style: TextStyle(color: ColorManager.color437EFF, fontSize: 11.sp, height: 1.5.w),
                              // 设置点击事件
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                 logic. getUserInfo(ContentType.personalCredit);
                                },
                            ),
                          ],
                        ),
                        style: TextStyle(
                          color: ColorManager.color999999,
                          fontSize: 13.sp,
                          height: 1.5,
                        ),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ],
                ),
              );
            }),
            setBottomButton(
              context,
              '提交',
              hasShadow: false,
              onPressed: logic.state.isComplete
                  ? () async {
                logic.submit();

              }
                  : null,
            ),
          ],
        ),
        body: Container(
          height: double.infinity,
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                ColorManager.colorFBE9DD,
                ColorManager.colorF4F5F6,
                ColorManager.colorF4F5F6,
              ],
            ),
          ),
          child: SingleChildScrollView(
            child: Column(
              children: [
                GetBuilder<IdentityCertificationLogic>(builder: (logic) {
                  return Stack(
                    children: [
                      Container(
                        margin: EdgeInsets.only(top: 30.w, left: 16.w, right: 14.w),
                        child: Row(
                          children: [
                            InkWell(
                              onTap: () {
                                state.percent = 280 * 0.3;
                                logic.update();
                              },
                              child: Container(
                                child: Text(
                                  '总进度',
                                  style: TextStyleUtils.style343D54(fontSize: 14.sp, fontWeight: FontWeight.bold),
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 10.w,
                            ),
                            Stack(
                              children: [
                                // 下面的背景盒子
                                Container(
                                  height: 4.w,
                                  width: 280.w,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(2.w)),
                                    color: ColorManager.colorFFFFFF,
                                  ),
                                ),
                                // 上面的进度条盒子
                                Container(
                                  height: 4.w,
                                  width: state.percent, // 动态更改进度条盒子的宽度即可
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(15.w)),
                                    color: ColorManager.colorD93C3A,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Visibility(
                        visible: state.proState,
                        child: Container(
                          decoration: const BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage("assets/icon_pro.png"),
                              fit: BoxFit.cover,
                            ),
                          ),
                          width: 25.w,
                          height: 15.w,
                          alignment: Alignment.center,
                          margin: EdgeInsets.only(
                            left: state.percent + 55.w,
                            top: 18.w,
                          ),
                          padding: EdgeInsets.only(bottom: 2.w),
                          child: Text(
                            state.proText,
                            style: TextStyleUtils.styleFFFFFF(fontSize: 10.sp),
                          ),
                        ),
                      ),
                      Container(
                        height: 280.w,
                        margin: EdgeInsets.only(top: 60.w, left: 16.w, right: 16.w),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(5.w)),
                          color: ColorManager.colorFFFFFF,
                        ),
                        child: GetBuilder<IdentityCertificationLogic>(builder: (logic) {
                          return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                margin: EdgeInsets.only(top: 15.w, left: 13.w, right: 13.w),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    state.idCardFront
                                        ? Image.memory(
                                      state.idCardFrontImageBytes,
                                      width: 150.w,
                                      height: 100.w,
                                      fit: BoxFit.cover,
                                    )
                                        : Image.asset(
                                      'images/auth_id_front.png',
                                      width: 156.w,
                                      height: 100.w,
                                    ),
                                    state.idCardBack
                                        ? Image.memory(
                                      state.idCardBackImageBytes,
                                      width: 150.w,
                                      height: 100.w,
                                      fit: BoxFit.cover,
                                    )
                                        : Image.asset(
                                      'images/auth_id_back.png',
                                      width: 156.w,
                                      height: 100.w,
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                margin: EdgeInsets.only(left: 13.w, right: 13.w),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    ClickWidget(
                                      onTap: () {
                                        if (!logic.checkTokenStatus()) return;

                                        BaiduOcrPlugin.instance.recognizeIdCardFrontNative(RecognizeCallbackHandler(
                                            onStart: logic.idCardFrontHandleStart,
                                            onResult: logic.idCardFrontHandleJsonRes,
                                            onError: logic.handleOcrError));
                                      },
                                      child: Container(
                                        width: 150.w,
                                        height: 30.w,
                                        alignment: Alignment.center,
                                        decoration: BoxDecoration(
                                          borderRadius:
                                          BorderRadius.only(bottomLeft: Radius.circular(5.w), bottomRight: Radius.circular(5.w)),
                                          color: ColorManager.colorD93C3A,
                                        ),
                                        child: Text(
                                          '拍摄正面',
                                          style: TextStyleUtils.styleFFFFFF(fontSize: 12.sp),
                                        ),
                                      ),
                                    ),
                                    ClickWidget(
                                      onTap: () {
                                        if (!logic.checkTokenStatus()) return;

                                        BaiduOcrPlugin.instance.recognizeIdCardBackNative(RecognizeCallbackHandler(
                                            onStart: logic.idCardBackHandleStart,
                                            onResult: logic.idCardBackHandleJsonRes,
                                            onError: logic.handleOcrError));
                                      },
                                      child: Container(
                                        width: 150.w,
                                        height: 30.w,
                                        alignment: Alignment.center,
                                        decoration: BoxDecoration(
                                          borderRadius:
                                          BorderRadius.only(bottomLeft: Radius.circular(5.w), bottomRight: Radius.circular(5.w)),
                                          color: ColorManager.colorD93C3A,
                                        ),
                                        child: Text(
                                          '拍摄背面',
                                          style: TextStyleUtils.styleFFFFFF(fontSize: 12.sp),
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              Container(
                                  margin: EdgeInsets.only(left: 10.w, right: 10.w, top: 15.w, bottom: 10.w),
                                  child: Text(
                                    '拍摄要求',
                                    style: TextStyleUtils.style343D54(fontSize: 12.sp, fontWeight: FontWeight.bold),
                                  )),
                              Container(
                                margin: EdgeInsets.only(left: 10.w, right: 10.w),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    buildColumn('images/auth_id_require_1.png', '标准'),
                                    buildColumn('images/auth_id_require_2.png', '边框缺失'),
                                    buildColumn('images/auth_id_require_3.png', '照片模糊'),
                                    buildColumn('images/auth_id_require_4.png', '闪光强烈'),
                                  ],
                                ),
                              )
                            ],
                          );
                        }),
                      ),
                    ],
                  );
                }),
                Container(
                  height: 280.w,
                  margin: EdgeInsets.only(top: 10.w, left: 16.w, right: 16.w),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(5.w)),
                    color: ColorManager.colorFFFFFF,
                  ),
                  child: Column(
                    children: [
                      Expanded(child: buildRow('姓名', state.nameController, '上传身份证后自动识别')),
                      Expanded(child: buildRow('身份证号', state.cardController, '上传身份证后自动识别')),
                      Expanded(child: buildRow('有效期', state.timeController, '上传身份证后自动识别')),
                      Expanded(child: buildRow('籍贯', state.placeController, '上传身份证后自动识别')),
                      Expanded(child: buildRow('发证机关', state.issuingAuthController, '上传身份证后自动识别')),
                      Expanded(child: buildRow('民族', state.nationController, '上传身份证后自动识别')),

                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget buildRow(String name, TextEditingController controller, String title) {
    return Container(
      margin: EdgeInsets.only(left: 15.w, right: 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            name,
            style: TextStyleUtils.style000000(fontSize: 15.sp),
          ),
          Expanded(
            child: TextField(
              controller: controller,
              maxLines: 10,
              minLines: 1,
              autocorrect: true,
              cursorColor: ColorManager.color353535,
              textAlign: TextAlign.right,
              keyboardType: TextInputType.text,
              textAlignVertical: TextAlignVertical.center,
              style: TextStyleUtils.style000000(fontSize: 15.sp),
              decoration: InputDecoration(
                isDense: true,
                contentPadding: EdgeInsets.only(bottom: 0.w),
                border: InputBorder.none,
                hintText: title,
                hintStyle: TextStyleUtils.style999999(fontSize: 15.sp),
              ),
              onChanged: (text) => LogUtils.d('change $text'),
              onSubmitted: (text) => LogUtils.d('submit $text'),
              enabled: true,
            ),
          )
        ],
      ),
    );
  }

  Column buildColumn(String image, String title) {
    return Column(
      children: [
        Image.asset(
          image,
          height: 55.w,
          width: 78.w,
        ),
        SizedBox(
          height: 4.w,
        ),
        Text(
          title,
          style: TextStyleUtils.style7F7F7F(fontSize: 12.sp),
        )
      ],
    );
  }
}
